package cn.jn.lhm.netty.part08.chat.rpc;

import cn.jn.lhm.netty.part08.chat.handler.RpcHandler;
import cn.jn.lhm.netty.part08.chat.protocol.ProtocolFrameDecoder;
import cn.jn.lhm.netty.part08.chat.protocol.SharableMessageCodec;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/11/2 - 23:03
 **/
@Slf4j
public class RpcServer {
    public static void main(String[] args) throws InterruptedException {
        NioEventLoopGroup boos = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();


        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(boos, worker);
        serverBootstrap.channel(NioServerSocketChannel.class);
        serverBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel client) throws Exception {
                client.pipeline().addLast(new ProtocolFrameDecoder());
                client.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                client.pipeline().addLast(new SharableMessageCodec());
                client.pipeline().addLast(new RpcHandler());
            }
        });
        ChannelFuture future = serverBootstrap.bind(8080).sync();
        future.channel().closeFuture().sync();
        boos.shutdownGracefully();
        worker.shutdownGracefully();


    }
}
